package c3g;

import com.sun.javadoc.*;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import java.io.*;
import java.util.ArrayList;


public class Reflex {
    //反射过程
    private ArrayList<Rex> list =new ArrayList<>();
    public void  show(RootDoc rootDoc,Rex Rex)  {
        String imports="";
        String sbr="";
        ClassDoc mes=null;
        ClassDoc[] classes = rootDoc.classes();
        //反射得到类
        for(ClassDoc classDoc : classes ){
            if(Rex.getType().equals("")){
                Rex.setType(classDoc.toString());
                Rex.setName(Rex.getType().split("\\."));
            }
            ClassDoc[] methodDocs = classDoc.innerClasses();
            //反射得到类中的方法
            for(ClassDoc publics :methodDocs){
                Rex Rex1 =new Rex();
                ClassDoc me = publics;
                MethodDoc[] e =  me.methods();
                //得到方法中的成员
                for(MethodDoc ee :e){
                    //得到成员下的注释
                    String sum =ee.commentText();
                    //判断是否有注释

                    if(sum.equals("")){
                        break;
                    }
                    if(me != mes){
                        String mess[] =publics.toString().split("\\.");
                        String message=mess[2].substring(0,mess[2].indexOf("OrBuilder"));
                        Rex1.getData().append("message ").append(message).append("{"+"\r");
                        mes=me;
                    }
                    //判断是否重复
                    if(!sum.equals(sbr)){
                                        //进行切割获取需要的部分
                        boolean iscode =sum.contains("<code>");
                            String[] code = sum.split("<code>");
                            String[] code1 =code[1].split("</code>");
                        boolean status =sum.contains("<pre>");
                        Rex1.setLabel(code1[0].substring(9,15));
                        if(Rex1.getLabel().equals(".core.")){
                            String []core =code1[0].split("\\.");
                            if(!core[2].equals(imports)){
                                Rex1.setImpor(core[2]);
                                imports=core[2];
                            }
                            else {
                                Rex1.setImpor("");
                            }
                        }
                        if(status){
                            String[] pre =sum.split("<pre>");
                            String[] pre1=pre[1].split("</pre>");
                            String comtxt =pre1[0].replace("\n","");
                            Rex1.getData().append(code1[0]).append("//").append(comtxt).append("\r");
                        }
                        else {
                            Rex1.getData().append(code1[0]).append("\r");
                        }
                        sbr =ee.commentText();
                    }
                }
                if(!Rex1.getData().toString().equals("")){
                    this.list.add(Rex1);
                    Rex1.getData().append("\r"+"}");
                }
            }
        }
    }
    public void build(Rex Rex,String protovm,String path) throws IOException {

        //判断out文件夹是否存在，不存在则创建
        File desfile =new File(path);
        if(!desfile.exists()){
            desfile.mkdir();
        }
        desfile.createNewFile();
        //设置模板、容器
        VelocityEngine ve =new VelocityEngine();
        VelocityContext velocityContext =new VelocityContext();  //生成对象
        ve.init();
        Template template =ve.getTemplate(protovm);   //设置模板文件
        velocityContext.put("Rex",Rex);  //设置容器
        velocityContext.put("list",this.list);
        StringWriter stringWriter =new StringWriter();
        template.merge(velocityContext,stringWriter);
        String r =stringWriter.toString();
        //生成文件

        Writer wr = new FileWriter(path+"/"+Rex.getName()[1]+".proto");
        wr.append(r);
        wr.flush();
        wr.close();
    }
}
